package com.yupi.springbootinit.manager;

import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.yucongming.dev.client.YuCongMingClient;
import com.yupi.yucongming.dev.common.BaseResponse;
import com.yupi.yucongming.dev.model.DevChatRequest;
import com.yupi.yucongming.dev.model.DevChatResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AiManager {

    @Resource
    private YuCongMingClient yuCongMingClient;

    /**
     * 鱼聪明
     * AI 对话
     *
    * */
    public String doChat(Long modelId, String message){

        // 构造请求参数
        DevChatRequest devChatRequest = new DevChatRequest();

        // 模型Id，尾后加 L 转成 long 类型
        devChatRequest.setModelId(modelId);
        devChatRequest.setMessage(message);

        // 获得相应结果
        BaseResponse<DevChatResponse> response = yuCongMingClient.doChat(devChatRequest);

        // 如果相应为 null ，就抛出系统异常提示 "AI 相应错误"
        if (response == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "AI 相应异常");
        }

        return response.getData().getContent();
    }
}
